# -*- coding: utf-8 -*-

from kivy.uix.boxlayout import BoxLayout

from kivy.properties import NumericProperty
from model import gamestate
from model.gamestate import Players
from ui.hat_widget import HatWidget


class PlayerNameLayout(BoxLayout):
    """ Class for labels with players' names
    """

    def __init__(self, root, index, name, **kwargs):
        self.root = root
        self.index = index
        self.name = name
        super(PlayerNameLayout, self).__init__(**kwargs)

    def delete_player(self, index):
        for word in self.root.game_state.hat.words:
            if word.owner is not None:
                if word.owner.name == self.name:
                    del word
        del (self.root.game_state.players.players[index])
        self.root.update()


class StartGame(HatWidget):
    """ Class for Start Game screen

        Start Game screen lists all connected players. The user can add
        player and start the game, when number of players is even.

        Does not change the gamestate
        Needs:
            gamestate has players attribute,
            list of players either exists or is None
        Does:
            Shows list of connected players
        Buttons:
            'Add Player' -> Add Player screen
            'Start!' -> Players Order screen
    """

    def __init__(self, game_state, presenter, **kwargs):
        """ Sets up the screen and saves game state and main presenter

            Args:
                game_state: (GameState) current state of the game
                presenter: (Presenter)  master presenter
                **kwargs: ()            misc arguments for widget creation
            Returns:
                None
        """

        super(StartGame, self).__init__(**kwargs)

        self.presenter = presenter
        self.game_state = game_state
        if game_state.players is None:
            game_state.players = Players([])

        #self.players_list.bind(minimum_height=
        #                       self.players_list.setter('height'))
        self.update()

    def update(self):
        """ Updates the list of players when it changes
        """

        self.players_list.clear_widgets()
        if self.game_state.players is None:
            self.game_state.players = Players([])
        players = self.game_state.players.players
        index = 0
        for player in players:
            self.players_list.add_widget(PlayerNameLayout(self, index,
                                                          player.name))
            index += 1
        del index
        if len(players) < 2:
            self.start_btn.enabled = False
        self.count = len(players)

    def start_btn_released(self):
        """ Handles the event when start button is released
        """
        players = self.game_state.players.players
        self.start_game()

    def start_game(self):
        """ Go to the next screen which displays the first playing pair
        """
        self.presenter.change_phase(gamestate.Phase.PLAYERS_ORDER)

    def go_back(self):
        """ Go to the previous screen """
        self.presenter.change_phase(gamestate.Phase.GAME_CONFIG)

    def add_player(self):
        """ Go to the next screen which displays the Add Player screen
        """
        self.presenter.change_phase(gamestate.Phase.ADD_PLAYER)
